﻿KISSY.add("fgt/auth/loginDialog", function (S, DOM, Event, UA, Node, Overlay, Validation, Ajax) {
    function loginDialog() {
        var self = this;
        self._init("#lg_popup", "#lgp_frm", {});
    }
    S.augment(loginDialog, {
        _init: function (id, formID, config) {
            var self = this, $ = S.all;
            self.id = id;
            self.form = $(formID);
            self.config = config;
            self.submitBtn = $("#lgp_submit");
            self.submitBtn.on("click", self.onSumbitBtnClick, self);

            self._initDialog();
            self._initValidator();
            self.msgBox = $("#lgp_err_msg");
            self.allMsgBoxes = self.form.all(".valid-simple");
        },
        _initDialog: function () {
            var self = this;
            self.dialog = new Overlay.Dialog({
                prefixCls: "fgt-",
                srcNode: self.id,
                elStyle: {
                    position: UA.ie == 6 ? "absolute" : "fixed"
                },
                closable: true,
                width: 440,
                mask: true,
                draggable: false,
                aria: true,
                zIndex: 999999
            });
            self.dialog.on("beforeVisibleChange", self.onBeforeVisibleChange, self);
            self.dialog.on("hide", self.onDialogClose, self);
            self.dialog.render();
            self.dialog.get("contentEl").on("keypress", function (event) {
                if (event.keyCode == S.Event.KeyCodes.ENTER) {
                    self.submitBtn.fire("click");
                }
            });
        },

        _initValidator: function () {
            var self = this,
                validator = new Validation(self.form[0], { warn: "Simple", style: "s_b" });
            validator.add("#lgp_act", { required: [true, "请输入帐号"] });
            validator.add("#lgp_pwd", { required: [true, "请输入密码"] });
            self.validator = validator;
        },

        onBeforeVisibleChange: function (event) {
            var self = this;
            if (event.newVal) {
                self.cleanMsgbox();
            }
        },

        cleanMsgbox: function () {
            var self = this;
            self.msgBox.hide();
            self.allMsgBoxes.removeClass("error")
            self.allMsgBoxes.text("");
        },

        show: function (successCallback, closeCallback, context, successCallbackParam, closeCallbackParam) {
            var self = this;
            self.callbackContext = context || self;
            self.successCallbackParam = successCallbackParam;
            self.closeCallbackParam = closeCallbackParam;

            if (S.isFunction(successCallback) === true) {
                self.successFn = successCallback;
            }
            else {
                self.successFn = undefined;
            }
            if (S.isFunction(closeCallback) === true) {
                self.closeFn = closeCallback;
            }
            else {
                self.closeFn = undefined;
            }
            self.dialog.center();
            self.dialog.show();
        },

        onDialogClose: function (event) {
            var self = this, closeFn = self.closeFn;
            if (S.isFunction(closeFn) === true) {
                closeFn.call(self.callbackContext);
            }
        },

        onSumbitBtnClick: function (event) {
            event.stopPropagation();
            var self = this, $ = S.all, j = jQuery,
                b = self.validator.isValid(),
                resultCode = FGT.core.constant.jsonResultCode;

            if (b === false) {
                return;
            }

            j.ajax({
                url: "/account/ajaxlogin",
                type: "POST",
                context: self,
                data: Ajax.serialize(self.form),
                beforeSend: function (jqXHR, settings) {
                    var self = this;
                    self.submitBtn.prop("disabled", true);
                },
                success: function (data, textStatus, jqXHR) {
                    var self = this;
                    if (data.Result === resultCode.SUCCESS) {
                        self.submitBtn.prop("disabled", false);
                        self.dialog.hide();
                        if (S.isFunction(self.successFn) === true) {
                            self.successFn.apply(self.callbackContext, self.successCallbackParam || []);
                        }
                    }
                    else {
                        var jsonData = data.Content;
                        if (S.isString(jsonData.ErrorMsg)) {
                            self.msgBox.addClass("error");
                            self.msgBox.show();
                            self.msgBox.text(jsonData.ErrorMsg);
                        }
                        else if (S.isArray(jsonData)) {
                            S.each(jsonData, function (item) {
                                var labelItem = $(item.id);
                                labelItem.addClass("error");
                                labelItem.text(item.msg);
                            });
                        }
                        self.submitBtn.prop("disabled", false);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var self = this;
                    self.cleanMsgBox();
                    self.msgBox.text("登录失败，请稍候再试");
                    self.submitBtn.prop("disabled", false);
                }
            });

        }
    });

    return loginDialog;
}, { requires: ["dom", "event", "ua", "node", "overlay", "validation", "ajax"] });


KISSY.add("fgt/auth", function (S, LoginDialog) {
    var auth = {
        getLoginDialog: function () {
            if (S.isUndefined(FGT.globalObj.loginDlg)) {
                FGT.globalObj.loginDlg = new LoginDialog();
            }
            return FGT.globalObj.loginDlg;
        },

        validate: function (passCallBack, popbox, context, successCallbackParam) {
            var self = this,
                resultCode = FGT.core.constant.jsonResultCode,
                loginDialog;

            jQuery.ajax({
                url: "/account/validate",
                type: "GET",
                cache: false,
                //                beforeSend: function (jqXHR, settings) {
                //                    popbox.show("验证中...");
                //                },
                success: function (data, textStatus, jqXHR) {
                    popbox.hide();
                    if (data.Result === resultCode.SUCCESS) {
                        passCallBack.apply(context || self, successCallbackParam || []);
                    }
                    else {
                        loginDialog = self.getLoginDialog();
                        loginDialog.show(passCallBack, undefined, context, successCallbackParam);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    popbox.show("验证错误，请稍候再试。", 1000);
                }
            });
        }

    };
    S.FGT.auth = auth;

    return auth;
}, { requires: ["./auth/loginDialog"] });